import GlobalEvent from "../cfw/event/GlobalEvent";
import TimeHelper from "../cfw/time/TimeHelper";
import GlobalTimeManager from "../cfw/time/GlobalTimeManager";
import SheduleTimeManager from "../cfw/time/SheduleTimeManager";


export default class Timer {
    private pauseTime: number = 0;
    constructor() {
        GlobalEvent.instance().on(GlobalEvent.EVENT_HIDE, this.eventHide, this)
        GlobalEvent.instance().on(GlobalEvent.EVENT_SHOW, this.eventShow, this)
    }

    eventShow() {

        console.log("游戏进入前台");
        this.pauseTime = TimeHelper.now() - this.pauseTime;
        this.start();

    }

    eventHide() {

        console.log("游戏进入后台");
        this.stop();
        this.pauseTime = TimeHelper.now()

    }

    stop() {
        // Laya.timer.clear(this, this.updateSecond)
        // Laya.timer.clear(this, this.updateShedule)
        cc.director.getScheduler().unschedule(this.updateShedule, this)
        cc.director.getScheduler().unschedule(this.updateSecond, this)
    }
    start() {
        // Laya.timer.loop(1000, this, this.updateSecond)
        // Laya.timer.frameLoop(1, this, this.updateShedule)
        cc.director.getScheduler().schedule(this.updateShedule, this, 1 / 60)
        cc.director.getScheduler().schedule(this.updateSecond, this, 1)
        // cc.tween(this).to(1, {}).call(this.updateSecond).repeatForever().start()
        // cc.tween(this).to(1 / 60, {}).call(this.updateShedule).repeatForever().start()
    }

    /**
     * 一帧走一次
     */
    updateShedule() {
        console.log(' cc.director.getDeltaTime() ', this.pauseTime)
        SheduleTimeManager.instance().update(cc.director.getDeltaTime() - this.pauseTime)
        this.pauseTime = 0;
    }

    /**
     * 一秒走一次
     */
    updateSecond() {
        GlobalTimeManager.instance().update(1)
    }




}